* rifregtable.do	JEP		01/26/15
* Builds regression tables for term paper version, updated from regtable.do

*************************** Change log ********************************** 

/*
	01/27/15	Add wrappers to generate stand-alone pdf
*/	

cap program drop rifregtable
program rifregtable
	syntax, nrif(integer) time(string) panels(string) pctiles(numlist) master(string) outdir(string) mw(string) per(string) [dom(string) new(string) wrap makepdf caption(string)]  

	use "`master'/Data/Source/ocupados_mw.dta", clear
	* Find different panels from file names
	* For robustness checks, only the two last panels
	foreach pnl in `panels' {
		glo cols ""
		glo head ""
		glo betas ""
		glo ses ""
		glo ps ""
		glo ns ""
		glo colnum=1
		foreach j in `pctiles' {
			* Centered columns
			glo cols "${cols}c"
			est use "`master'/Ster/rif`nrif'`new'/reg_`pnl'_tq(`time')_`mw'_salario_`per'_real_50`dom'_p`j'.ster"
			glo b:  di %10.4f _b[treat]
			glo se :  di %10.4f _se[treat]
			glo p=e(tp)
			if $p<0.001 glo star "***"
			else if $p<0.05 glo star "**"
			else if $p<0.1 glo star "*"
			else glo star ""
			glo p:  di %10.3f $p
			glo n= e(N)
			glo head "${head} & `j'"
			glo betas "${betas} & ${b}${star}"
			glo ses "${ses} & ($se )"
			glo ps "${ps} & [$p ]"
			glo ns "${ns} & $n"
			glo colnum = ${colnum}+1
			
			if ("`time'"=="1999q1" & "`pnl'"=="emp") | ("`time'"=="1998q4" & "`pnl'"=="sst_emp_bp")  loc r "replace" 
			else loc r "append"
			
			if "`pnl'"=="emp" glo title "Panel A. Employment"
			if "`pnl'"=="emp_bp" glo title "Panel B. Employment and Bartik Price Variable "
			if "`pnl'"=="emp_bq" glo title "Panel C. Employment and Bartik Quantity Variable "
			if "`pnl'"=="sst_emp" glo title "Panel D. Employment and City Trends "
			if "`pnl'"=="sst_emp_bp" glo title "Panel E. Employment, Bartik Price Variable and City Trends "
			if "`pnl'"=="sst_emp_bq" glo title "Panel F. Employment, Bartik Quantity Variable and City Trends "
		}
		if ("`time'"=="1999q1" & "`pnl'"=="emp") | ("`time'"=="1998q4" & "`pnl'"=="sst_emp_bp")  loc r "replace" 
		else loc r "append"

		if "`pnl'"=="emp" glo title "Panel A. Employment"
		if "`pnl'"=="emp_bp" glo title "Panel B. Employment and Bartik Price Variable "
		if "`pnl'"=="emp_bq" glo title "Panel C. Employment and Bartik Quantity Variable "
		if "`pnl'"=="sst_emp" glo title "Panel D. Employment and City Trends "
		if "`pnl'"=="sst_emp_bp" glo title "Panel E. Employment, Bartik Price Variable and City Trends "
		if "`pnl'"=="sst_emp_bq" glo title "Panel F. Employment, Bartik Quantity Variable and City Trends "


		* cd "C:/Users/jorge_perez/Desktop/AA_Minimum_Wage/Doc/reg"
		texdoc init `outdir'/`nrif'_`time'.tex, `r'
		if ("`time'"=="1999q1" & "`pnl'"=="emp") | ("`time'"=="1998q4" & "`pnl'"=="sst_emp_bp") {
			if "`wrap'"=="wrap" {
				tex \documentclass{article}
				tex \usepackage[letterpaper,left=3cm,right=3cm,top=3cm,bottom=3cm]{geometry}
				* tex \usepackage{adjustbox}
				tex \begin{document}
				tex \begin{table}
				tex \caption{`caption'}
				tex \centering
				tex \footnotesize
				* tex \begin{adjustbox}{center}
			}
			tex \begin{tabular}{l${cols}}
		}
		* p5 p10 p15 p20 p25 p50 p75 p90
		tex \multicolumn{$colnum}{l}{$title} \\\hline
		tex Percentile ${head} \\\hline
		tex Coef $betas \\
		tex Se $ses \\
		tex P-Value $ps \\
		tex N $ns \\\hline
		tex \\
		texdoc close
		
	}
	texdoc init `outdir'/`nrif'_`time'.tex, append
	tex \end{tabular}
	if "`wrap'"=="wrap"{ 
		* tex \end{adjustbox
		tex \end{table}
		tex \end{document}
	}
	texdoc close
	
	if "`makepdf'"=="makepdf" {
		cd "`outdir'"
		shell pdflatex `outdir'/`nrif'_`time'.tex
	}

end

